package com.caishi.lkx.exam.service.impl;

import cn.hutool.core.util.StrUtil;
import com.caishi.lkx.exam.mapper.PaperRecordsMapper;
import com.caishi.lkx.exam.model.PaperRecordsModel;
import com.caishi.lkx.exam.service.IExColumnConfigReService;
import com.caishi.lkx.exam.service.IPaperRecordsService;
import com.caishi.lkx.exam.vo.QuestionCollectVo;
import com.zzw.common.entity.IBSDMapper;
import com.zzw.common.threadpool.SysThreadPool;
import com.caishi.lkx.exam.ienum.type.ColumnType;
import com.caishi.lkx.exam.ienum.type.PaperRecordType;
import com.caishi.lkx.exam.ienum.type.QuestionCollectionType;

import com.caishi.lkx.exam.records.RecordsCollectType;
import com.caishi.lkx.exam.records.RecordsGetDataService;
import com.caishi.lkx.exam.records.dto.CollectionErrorAndCollectRecordDataDto;
import com.caishi.lkx.exam.records.dto.QuestionRecordsDto;

import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Future;

@Service
public class PaperRecordsServiceImpl extends AbstractQuestionCollectionService<PaperRecordsModel, String> implements IPaperRecordsService {

    @Resource
    private PaperRecordsMapper mapper;

    @Resource
    private RecordsGetDataService recordsGetDataService;

    @Resource
    private IExColumnConfigReService columnConfigReService;

    @Override
    public IBSDMapper<PaperRecordsModel, String> getMapper() {
        return mapper;
    }

    @SneakyThrows
    @Override
    public QuestionCollectVo clientCollectionData(String userId, Long groupId, String id, String industryId, String examId, Long categoryId,boolean setRightAnswer) {
        Future<QuestionRecordsDto> future = null;
        Future<CollectionErrorAndCollectRecordDataDto> collectFuture = null;
        var model = getById(id);
        var recordType = RecordsCollectType.paperRecordType2Type(model.getType());
        if (StrUtil.isNotEmpty(userId)) {
            future = SysThreadPool.submit(() -> {
//                if (recordType == RecordsCollectType.paperRecordChapterPractice) {
//                    return new QuestionRecordsDto();
//                }
                return recordsGetDataService.getQuestionRecordsResult(userId, industryId, examId, categoryId, recordType,  model.getType() == PaperRecordType.chapterPractice ? model.getPaperId() : id);
            });
            collectFuture = SysThreadPool.submit(() -> recordsGetDataService.getCollectInCollectDataResult(userId, industryId, examId, categoryId,
                    recordType,
                    model.getType() == PaperRecordType.paperLibrary || model.getType() == PaperRecordType.chapterPractice ? model.getPaperId() : id));
        }
        var result = super.clientCollectionData(userId, groupId, id, industryId, examId, categoryId,true);
        if (future != null) {

            coreQuestionCollectionService.safeProcess(future.get(), collectFuture.get(), result, userId, id, setRightAnswer);
        } else if (groupId != null) {
            coreQuestionCollectionService.safeProcess(new QuestionRecordsDto(), new CollectionErrorAndCollectRecordDataDto(), result, groupId.toString(), id, false);
        }
        result.setRecordsType(recordType);
        return result;
    }

    @Override
    QuestionCollectionType nowCollectionType() {
        return QuestionCollectionType.paper;
    }

    @Override
    public PaperRecordsModel userNotFinishPaperRecords(String userId, PaperRecordType columnType, String paperId) {
        if (columnType == PaperRecordType.paperLibrary) {
            return selectFirst(wrappers().eq(PaperRecordsModel::getUserId, userId)
                    .eq(PaperRecordsModel::getPaperId, paperId)
                    .eq(PaperRecordsModel::getType, PaperRecordType.paperLibrary)
                    .isNull(PaperRecordsModel::getEndTime)
            );
        }
        return selectFirst(wrappers().eq(PaperRecordsModel::getUserId, userId)
                .eq(PaperRecordsModel::getType, columnType)
                .isNull(PaperRecordsModel::getEndTime)
        );
    }

    @Override
    public void delUserAllRecords(String userId, Long categoryId) {
        delete(wrappers().eq(PaperRecordsModel::getUserId, userId).eq(PaperRecordsModel::getCategoryId, categoryId));
    }

    @Override
    public int userFinishPaperCount(String userId, Long categoryId) {
        var categoryPaperIds = columnConfigReService.columnTypeConfigs(categoryId, ColumnType.paperLibrary);
        if (categoryPaperIds.isEmpty()) return 0;
        return (int) selectList(wrappers().eq(PaperRecordsModel::getUserId, userId)
                .select(PaperRecordsModel::getPaperId)
                .eq(PaperRecordsModel::getType, PaperRecordType.paperLibrary)
                .in(PaperRecordsModel::getPaperId, categoryPaperIds)
                .isNotNull(PaperRecordsModel::getEndTime)
        ).stream().map(PaperRecordsModel::getPaperId).distinct().count();
    }

    @Override
    public List<PaperRecordsModel> questionRecord(String userId, PaperRecordType paperRecordType, String paperId) {
        return mapper.questionRecord(userId, paperRecordType, paperId);
    }
}
